//
//  CPHomeViewController.swift
//  weibo
//
//  Created by zcp on 16/5/9.
//  Copyright © 2016年 zcp. All rights reserved.
//

import UIKit

class CPHomeViewController: CPBaseTableViewController {

    //MARK: - 属性
    //cell标识
    private let reuseIdentifier = "cell"
    //titleView
    private var titleBtn: UIButton?
    //微博数据属性
    var statuses: [CPStatus]?  {
    
        didSet{
        
            self.tableView.reloadData()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //判断是否登录,防止在没登录状态下,导航栏还显示下面设置的导航栏按钮
        if !CPUserAccountViewModel.sharedAccountViewModel.isLogin(){
        //返回不设置导航栏了
            return
        }
        //注册cell
        tableView.registerClass(CPStatusCell.self, forCellReuseIdentifier: reuseIdentifier)
        //设置预估高
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        //去除分隔线
        tableView.separatorStyle  = UITableViewCellSeparatorStyle.None
        tableView.allowsSelection = false
        //设置导航栏
       setNavitationBar()
        
        //加载微博数据
        CPStatusViewModel.sharedStatus.loadStatus { (status, error) -> () in
            if error != nil
            {
                //数据回调失败
                print("微博数据回调失败:\(error)")
            
                return
            }
            //请求数据成功,赋值
            self.statuses = status
        }
        

        }
    //MARK: - 设置导航栏
    private func setNavitationBar()
    {
    
        //设置导航栏两边的按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: "didLeftClilk")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "didRightClick")
        //设置导航栏的titleview
        let btn:UIButton = {
            let btn = CPNaviTitleViewBtn()
            //设置用户名称
            let userName = CPUserAccountViewModel.sharedAccountViewModel.userAccount?.screen_name ?? "未知用户"
            btn.setTitle(userName, forState: UIControlState.Normal)
            btn.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
            btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
            btn.sizeToFit()
            btn.addTarget(self, action: "titleViewClick:", forControlEvents: UIControlEvents.TouchUpInside)
            titleBtn = btn
            return btn
            }()
        navigationItem.titleView = btn
    }
    //MARK: - 在进入登录状态下导航栏两边按钮点击操作
    func didLeftClilk(){
        
        
        print("home-didLeftClilk")
    }
    func didRightClick(){
        
        
        print("home-didRightClick")
    }
    //titleView点击操作
    func titleViewClick(btn: UIButton){
    
        btn.selected = !btn.selected
        

        UIView.animateWithDuration(0.5) { () -> Void in
            let tranform = btn.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI)-0.0001): CGAffineTransformIdentity
            btn.imageView?.transform = tranform
        }
    }
}
    //MARK: - 扩展 数据源方法
extension CPHomeViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statuses?.count ?? 0
//        return 10
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CPStatusCell
//        cell.backgroundColor = UIColor.lightGrayColor()
        cell.status = self.statuses![indexPath.row]
        return cell
    }
}
